--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once
--开服特惠礼包


function SendOpenServerGiftData( sysarg )
	
	local sVar = Actor.getStaticVar(sysarg)

	if not sVar.openServerGiftData then
		sVar.openServerGiftData = {}
	end

	local giftCfg = OpenServerCfg.gift

	local netPack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sSendPreferentialGiftInfo)
	if netPack then
		local length = #giftCfg.giftData
		DataPack.writeChar(netPack, length)
		for idx, data in ipairs(giftCfg.giftData) do
			DataPack.writeChar(netPack, idx)
			if not sVar.openServerGiftData[idx] then
				sVar.openServerGiftData[idx] = {}
			end
			DataPack.writeChar(netPack, #data)
			for ii, preData in ipairs(data) do
				local State = sVar.openServerGiftData[idx][ii] or 0
				DataPack.writeChar(netPack, ii)
				DataPack.writeChar(netPack, State)
			end
		end
		DataPack.flush(netPack) 
	end

end

function BuyOpenServerGift( sysarg, packet )

	local giftCfg = OpenServerCfg.gift

	if not Actor.checkActorLevel(sysarg, giftCfg.nLevel, 0) then
		Actor.sendTipmsg( sysarg,string.format(Lang.ScriptTips.COM0055, giftCfg.nLevel),ttFlyTip )
		return
	end
	local idx = DataPack.readChar(packet)
	local ii = DataPack.readChar(packet)
	local ItemConfig = giftCfg.giftData[idx][ii]

	if not ItemConfig then
		return
	end

	local day = System.getDaysSinceOpenServer()
	if ItemConfig.openDay and ItemConfig.openDay > day then
		Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.OpenServer009, ItemConfig.openDay),ttFlyTip )
		return
	end

	if ItemConfig.endDay and ItemConfig.endDay < day then
		Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.OpenServer014, ItemConfig.endDay),ttFlyTip )
		return
	end
	
	local sVar = Actor.getStaticVar(sysarg)


	if not sVar.openServerGiftData then
		sVar.openServerGiftData = {}
	end

	if not sVar.openServerGiftData[idx] then
		sVar.openServerGiftData[idx] = {}
	end

	if ii > 1 then
		for iii = 1, ii - 1 do
			local PreItemConfig = giftCfg.giftData[idx][iii]
			local preOpenDay = PreItemConfig.openDay or 1
			local preEndDay = PreItemConfig.endDay or OpenServerCfg.OpenServerDay
			if not ((sVar.openServerGiftData[idx][iii] and sVar.openServerGiftData[idx][iii] == 1) or
				preOpenDay > day or preEndDay < day) then
				Actor.sendTipmsg( sysarg,Lang.ScriptTips.OpenServer008,ttFlyTip )
				return
			end
		end
	end

	if sVar.openServerGiftData[idx][ii] == 1 then
		return
	end

	if CheckBagGridForAwardsEx(sysarg,ItemConfig.awards) > 0 then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.COM0015, ttFlyTip )
		return
	end

	if Actor.getMoneyCount(sysarg, mtYuanbao) < ItemConfig.yuanbao then
		Actor.sendItemLack( sysarg, qatYuanbao, 0, ItemConfig.yuanbao )
		--Actor.sendTipmsg( sysarg,string.format(Lang.ScriptTips.COM0053, ItemConfig.yuanbao),ttFlyTip )
	end

	if not Actor.changeMoney(sysarg,mtYuanbao, -ItemConfig.yuanbao, 1096, Lang.LogTips.OpenServerGiftBag001) then
		return
	end

	sVar.openServerGiftData[idx][ii] = 1

	GiveCommonAwardEx(sysarg, ItemConfig.awards, 1096, Lang.LogTips.OpenServerGiftBag002)

	SendOpenServerGiftData(sysarg)

	local actorName = Actor.getName(sysarg)
	local msg = string.format(Lang.ScriptTips.OpenServer001, actorName, ItemConfig.sItemName)		
	System.broadcastTipmsg(msg, ttChatWindow)

end
